#' 代替library来载入package
#'
#' 如果package未安装，将自动安装
#'
#' @param pkg 带双引号的字符
#' @param quietly 同\code{require()}的参数quietly
use_ = function(pkg, quietly = FALSE) {
  rst = 0
  installed.pkg = as.data.frame(installed.packages(), stringsAsFactors = FALSE)
  installed.pkg = installed.pkg$Package
  loaded = pkg %in% devtools::loaded_packages()$package
  installed = pkg %in% installed.pkg
  if(loaded) {
    message("已载入package：", pkg)
  } else {
    message("未载入package：", pkg)
    if(installed) {
      message("正在载入package：", pkg, " ...")
      require(pkg, quietly = quietly, character.only = TRUE)
    } else {
      message("未安装package：", pkg, " !!!\n")
      message("尝试安装package：", pkg, " ...\n")
      rst = install.packages(pkg, quiet = TRUE)
      message("安装 ", pkg, " 成功！\n")
      message("正在载入package：", pkg, " ...\n")
      require(pkg, quietly = quietly, character.only = TRUE)
      message("载入package：", pkg, " 成功！！！\n")
    }
  }
  rst
}

#' 代替library来载入package
#'
#' 如果package未安装，将自动安装
#'
#' @param ... 带双引号的字符向量
#' @param quietly 同\code{require()}的参数quietly
use = function (..., quietly = FALSE) {
  pkgs = unlist(list(...))
  lapply(pkgs, function(w) {
    use_(w)
  })
}

#' 获取京东搜索页面n页的url
#'
#' @param remdr remoteDriver
#' @param keyword 搜索词
#' @param n_pages 默认为3，搜索的页数
#' @param max_pages 默认10，最大搜索页数
hg_rs_jd_search_n_pages_urls = function(remdr, keyword,
                                        n_pages=3, max_pages=10) {
  rst = hg_rs_jd_search_page_urls(remdr, keyword)
  message("\n====== 第【 1 】页 完成 ====== \n")
  # 总页数css
  css = ".p-skip > em:nth-child(1) > b:nth-child(1)"
  page = hg_rs_to_source(remdr)
  num = as.numeric(hg_html_element_text(page, css))
  if(num > 1 & n_pages > 1) {
    Sys.sleep(2)
    n = min(n_pages, max_pages)
    for(i in 2:n) {
      el = remdr$findElement("css", "input.input-txt:nth-child(2)")
      el$clearElement()
      el$sendKeysToElement(list(as.character(i), key = "enter"))
      Sys.sleep(5)
      hg_rs_scroll_attr_load(remdr, ".p-skip", "class", "p-skip", times = try)
      message("获取源代码...")
      page = hg_rs_remdr2page(remdr)
      message("生成第【",i,"】页的urls...")
      ids = hg_rs_get_css_attribute(page, "li.gl-item", "data-sku")
      urls = paste0("https://item.jd.com/",ids, ".html")
      message("\n====== 第【",i,"】页 完成 ====== \n")
      rst = c(rst, urls)
    }
  } else {
    unique(rst)
  }
  message("\n====== 全部 完成 ====== \n")
  unique(rst)
}

#' 获取京东搜索页面的urls
#'
#' @param remdr remoteDriver
#' @param keyword 关键词
#' @param try 滚动滚动条的次数，直到页面载入成功。
hg_rs_jd_search_page_urls = function(remdr, keyword, try = 10, url = NULL) {
  message("开始...")
  if(is.null(url)) url = "http://www.jd.com"
  message("打开url：", url)
  remdr$navigate(url)
  Sys.sleep(1)
  checked = hg_rs_scroll_attr_load(remdr, "input#key", "id", "key", times = 5)
  # 如果页面载入成功
  if(checked) {
    message("查找输入框...")
    el = remdr$findElement("id", "key")
    message("输入关键字：【",keyword,"】")
    Sys.sleep(1)
    el$clearElement()
    el$sendKeysToElement(list(keyword, key = "enter"))
    Sys.sleep(5)
    message("====== 搜索... ======")
    hg_rs_scroll_attr_load(remdr, ".p-skip", "class", "p-skip", times = try)
    message("获取源代码...")
    page = hg_rs_remdr2page(remdr)
    message("生成urls...")
    ids = hg_rs_get_css_attribute(page, "li.gl-item", "data-sku")
    urls = paste0("https://item.jd.com/",ids, ".html")
    message("\n====== 完成 ====== \n")
    return(urls)
  } else {
    return(NA)
  }
}

#' 检测标签的属性来判断页面是否加载完成
#'
#' @param remdr remoteDriver
#' @param css css选择器
#' @param attr_name 属性名称
#' @param value 用于与attr_name参数的属性值对比的文本
#' @param times 失败再尝试的次数
#' @param waiting 失败再尝试的时间间隔
hg_rs_scroll_attr_load = function (remdr, css, attr_name, value, times = 30, waiting = 1) {
  message("滚到底部...")
  hg_rs_scroll_bottom(remdr)
  checked = hg_rs_isloaded_attr(remdr, css, attr_name, value)
  message("checked: ", checked)
  if(checked) {
    message("随机滚动...")
    hg_rs_random_scroll(remdr)
    message("\n=== 载入完成。 ===\n")
    return(1)
  } else {
    for(i in 1:times) {
      message("剩余滚动次数：", times - i + 1)
      Sys.sleep(waiting)
      hg_rs_scroll_bottom(remdr)
      hg_rs_random_scroll(remdr)
      checked = hg_rs_isloaded_attr(remdr, css, attr_name, value)
      if(checked) {
        message("\n=== 载入完成。 ===\n")
        return(1)
      }
    }
    message("\n=== 载入可能失败 ===\n")
    return(0)
  }
}

#' 通过检测标签文本来判断页面是否加载完成
#'
#' @param remdr remoteDriver
#' @param css css选择器
#' @param value 用于比较的文本
#' @param times 失败再尝试次数
#' @param waiting 失败再尝试的时间间隔
hg_rs_scroll_node_load = function (remdr, css, value, times = 10, waiting = 1) {
  message("滚到底部...")
  hg_rs_scroll_bottom(remdr)
  checked = hg_rs_isloaded_node_text(remdr, css, value)
  message("checked: ", checked)
  if(checked) {
    message("随机滚动...")
    hg_rs_random_scroll(remdr)
    message("\n=== 载入完成。 ===\n")
    return(1)
  } else {
    for(i in 1:times) {
      message("剩余滚动次数：", times - i + 1)
      Sys.sleep(waiting)
      hg_rs_scroll_bottom(remdr)
      hg_rs_random_scroll(remdr)
      if(checked) {
        message("\n=== 载入完成。 ===\n")
        return(1)
      }
    }
    message("\n=== 载入可能失败 ===\n")
    return(0)
  }
}

#' 滚动到底端
#'
#' @param remdr remoteDriver
#' @param times 次数
#' @param waiting 时间间隔
hg_rs_scroll_bottom = function(remdr, times = 3, waiting = 0.5) {
  message("scroll to bottom...")
  for(i in 1:times) {
    js = "window.scrollTo(0, document.body.scrollHeight);
  window.document.body.scrollTop = document.body.scrollHeight;"
    remdr$executeScript(js, args = list("dummy"))
    Sys.sleep(waiting)
  }
}

# 滚动函数
hg_rs_scroll = function(remdr,
                        css = ".filter-list > li:nth-child(1)",
                        attribute = "data-num",
                        waiting = 3, times = 10) {
  message("滚动屏幕...")
  if(hg_rs_check_loaded(remdr, css, attribute)) {
    return(1)
  }
  js = "window.scrollTo(0, 10000000);window.document.body.scrollTop = document.body.scrollHeight;"
  remdr$executeScript(js, args = list("dummy"))
  count = 0
  checked = hg_rs_check_loaded(remdr, using, value)
  while (checked == 0 & count < times) {
    Sys.sleep(waiting)
    if(checked) return(1)
    remdr$executeScript(js, args = list("dummy"))
    count = count + 1
    checked = hg_rs_check_loaded(remdr, using, value)
    message(paste0("count = ", count, " , checked = ", checked))
  }
  hg_rs_random_scroll(remdr)
  ifelse(checked, 1, 0)
}


#' 随机滚动页面的滚动条
#'
#' @param remdr remoteDriver
hg_rs_random_scroll = function(remdr) {
  message("random scroll...")
  times = round(runif(1), 2) + 0.2
  hg_rs_scroll_ntimes_height(remdr, times, 1, 0.5)
}

#' Scroll to n times height
#'
#' @param remdr remoteDriver
#' @param n times
#' @param try retry times
#' @param waiting delay before retry
hg_rs_scroll_ntimes_height = function(remdr, n = 0.25, try = 3, waiting = 0.5) {
  message("scroll to [ ",n," ] times .body.scrollHeight...")
  for(i in 1:try) {
    tryCatch({
      suppressMessages({
        js = paste0("
                    var q = document.body.scrollHeight * ",n,";
                    window.scrollTo(0, q);
                    ")
        remdr$executeScript(js, args = list("dummy"))
      })
    },
    error = function(e) {
      message(e, "\n")
      return(0)
    })
  }
}

#' scroll to half doc height
#'
#' @param remdr remoteDriver
hg_rs_scroll_half = function (remdr) {
  message("scroll to half .body.scrollHeight...")
  tryCatch({
    suppressMessages({
      js = "
      var q = document.body.scrollHeight/2;
      window.scrollTo(0, q);
      "
      remdr$executeScript(js, args = list("dummy"))
      Sys.time(1)
    })
  },
  error = function(e) {
    message(e, "\n")
    return(0)
  })
}


hg_rs_random_scrolls = function(remdr,
                                times = 10,
                                sleeptime = 1) {
  message("随机滚动...")
  for(i in 1:times) {
    hg_rs_random_scroll(remdr)
    Sys.sleep(sleeptime)
    message("剩余滚动次数：", times - i + 1)
  }
}


#' 单次随机滚动滚动条
#'
#' @param remdr remoteDriver
hg_rs_random_scroll = function(remdr) {
  r = ifelse(runif(1, -1, 1) >= 0, 1, -1)
  h = round(runif(1, 10, 20)) * 100 * r
  js = paste0("window.scrollTo(0, ", h, ");")
  remdr$executeScript(js, args = list("dummy"))
}


#' 通过标签属性判断页面是否加载完毕
#'
#' @param remdr remoteDriver
#' @param css css选择器
#' @param attr_name 属性名称
#' @param value 用于与属性名称对应的文本比较的文本
hg_rs_isloaded_attr = function (remdr, css, attr_name, value) {
  page = hg_rs_to_source(remdr)
  checked = hg_html_attr_identical(page, css, attr_name, value)
  rst = ifelse(checked, TRUE, FALSE)
  on.exit({
    if(rst) message("=== 载入完毕.")
    else message("loading...")
  })
  rst
}

#' 通过标签文本判断页面是否加载完毕
#'
#' @param remdr remoteDriver
#' @param css css选择器
#' @param value 用于比较的文本
hg_rs_isloaded_node_text = function (remdr, css, value) {
  page = hg_rs_to_source(remdr)
  checked = hg_html_element_identical(page, css, value)
  rst = ifelse(checked, TRUE, FALSE)
  on.exit({
    if(rst) message("=== 载入完毕.")
    else message("loading...")
  })
  rst
}

# 检测载入完毕
hg_rs_check_loaded = function(remdr,
                              css = ".filter-list > li:nth-child(1)",
                              attribute = "data-num") {
  try({
    mypage <- remdr$getPageSource()
  }, silent = TRUE)
  page <- xml2::read_html(mypage[[1]])
  checked = hg_rs_get_css_attribute(page, css , attribute)
  message("check attribute = ", checked)
  if(is.na(checked))
    return(0)
  else
    return(1)
}

# 通过css获取属性值
hg_rs_get_css_attribute = function(page, css, attribute) {
  rst = character(0)
  try({
    rst = rvest::html_attr(rvest::html_nodes(page, css), attribute)
  }, silent = TRUE)
  if(length(rst)==0) NA
  else rst
}


hg_rs_remdr2page = function(remdr) {
  mypage <- remdr$getPageSource()
  xml2::read_html(mypage[[1]])
}


hg_rs_jd_products = function(remdr, keyword,
                             file,
                             n_pages = 3,
                             n_products = NULL,
                             max_pages = 10,
                             max_products = 100,
                             platform = "京东"){
  urls = hg_rs_jd_search_n_pages_urls(remdr, keyword, n_pages, max_pages)
  if(is.null(n_products)) {
    n = max_products
  } else {
    n = min(length(urls), n_products)
  }
  urls = urls[1:n]
  print(urls)
  rst = hg_rs_jd_product_rows(urls, file, keyword = keyword, platform = platform)
  rst
}

# 从源代码获取产品页面所有信息
hg_rs_jd_product_source_df = function(remdr) {
  mypage <- remdr$getPageSource()
  page <- xml2::read_html(mypage[[1]])
  message("product_df...")
  product = hg_rs_jd_product_df(page)
  message("hg_rs_jd_pinglun_df...")
  pinglun = hg_rs_jd_pinglun_df(page)
  message("hg_rs_jd_intro_all_df...")
  intro_all = hg_rs_jd_intro_all_df(page)
  bind_cols(
    product, pinglun, intro_all
  )
}

hg_rs_jdhk_product_all_df = function(remdr) {
  mypage <- remdr$getPageSource()
  page <- xml2::read_html(mypage[[1]])
  message("product_df...")
  product = hg_rs_jdhk_product_df(page)
  message("hg_rs_jd_pinglun_df...")
  pinglun = hg_rs_jdhk_pinglun_df(page)
  message("hg_rs_jd_intro_all_df...")
  intro_all = hg_rs_jdhk_intro_all_df(page)
  bind_cols(
    product, pinglun, intro_all
  )
}


hg_rs_jd_product_rows = function(urls, file, keyword = NA, platform = "京东") {
  pos = remdr$getWindowPosition(remdr$getCurrentWindowHandle())
  size = remdr$getWindowSize()
  message("宽度: ", size$width)
  message("高度: ", size$height)
  message("X: ", pos$x)
  message("Y: ", pos$y)
  n = length(urls)
  writedf = data.frame(stringsAsFactors = FALSE)
  count = 0
  rst = lapply(urls, function(w) {
    count <<-  count + 1
    left = n - count + 1
    message("\n===== 剩余页面数：",left," =====\n")
    wrst = hg_rs_jd_product_row(remdr, w)
    wrst[1, "排位"] = count
    wrst[1, "平台"] = platform
    wrst[1, "关键字"] = keyword
    writedf <<-  bind_rows(writedf, wrst)
    print(writedf[, c("排位", "商品名称")])
    if((count %% 5) == 0) {
      message("\n=== writing csv file... ===\n")
      csv = write.csv(writedf, file, row.names = FALSE)
    }
    wrst
  }) %>% invisible()
  on.exit({
    message("\n=== writing csv file... ===\n")
    try({
      csv = write.csv(writedf, file, row.names = FALSE)
    }, TRUE)
  })
  message("...... 完毕 ......")
  bind_rows(rst)
}

hg_rs_jd_product_row = function(remdr, url) {
  message("======== begin... ========")
  message(paste0("open：", url))
  remdr$navigate(url)
  hg_rs_random_scroll(remdr)
  #remdr$maxWindowSize()
  newurl = remdr$getCurrentUrl()
  isjd =  regexpr("jd.com", newurl) > 0
  isjdhk = regexpr("jd.hk", newurl) > 0
  if(isjd) {
    rst_one = hg_rs_jd_product_row_(remdr)
  } else if(isjdhk) {
    rst_one = hg_rs_jdhk_product_row_(remdr)
  }
  message("处理other info信息...")
  rst_two = hg_rs_jd_other_info_df(remdr)
  message("随机滚动、延时...")
  stime = round(runif(1, 1, 10))
  hg_rs_random_scrolls(remdr, stime)
  message("处理完毕。\n===========")
  bind_cols(rst_one, rst_two)
}

hg_rs_jd_product_row_ = function(remdr) {
  checked = hg_rs_scroll_attr_load(remdr, ".percent-con",
                                   "class", "percent-con", 30)
  if(!checked) {
    warning("网页未加载完毕!!!")
    remdr$screenshot(display = 1)
    return(NULL)
  }
  # remdr$screenshot(display = 1)
  message(paste0("title：", remdr$getTitle(), "\n"))
  message(paste0("url：", remdr$getCurrentUrl(), "\n"))
  message("======== 数据处理中... ========")
  message("处理product信息...")
  rst_one = hg_rs_jd_product_source_df(remdr)
  rst_one
}

hg_rs_jdhk_product_row_ = function(remdr, url) {
  checked = hg_rs_isloaded_node_text(remdr, ".rate > strong:nth-child(2) > span:nth-child(1)", "%")
  if(!checked) {
    warning("网页未加载完毕!!!")
    remdr$screenshot(display = 1)
    return(NULL)
  }
  # remdr$screenshot(display = 1)
  message(paste0("title：", remdr$getTitle(), "\n"))
  message(paste0("url：", remdr$getCurrentUrl(), "\n"))
  message("======== 数据处理中... ========")
  message("处理product信息...")
  rst_one = hg_rs_jdhk_product_all_df(remdr)
  rst_one
}




# 获取产品基本信息
hg_rs_jd_product = function(page) {
  title = html_node(page, ".sku-name") %>%
    html_text() %>%
    stringi::stri_replace_all_fixed("\n", "") %>%
    trimws()
  price = html_node(page, ".p-price") %>%
    html_text() %>%
    stringi::stri_replace_all_fixed("￥", "") %>%
    as.numeric()
  pinpai = hg_rs_get_css_attribute(page, "#parameter-brand li", "title")
  wuliu = html_node(page, "#summary-service .hl_red") %>%
    html_text()
  isjd = html_node(page, ".u-jd span") %>%
    html_text()
  shopname = html_node(page, ".J-hove-wrap > div:nth-child(1) > div:nth-child(1) > a:nth-child(1)") %>%
  html_text()
  ziying = ifelse(identical(isjd, "JD"), "自营", "否")
  list(标题 = title,
       价格 = as.numeric(price),
       品牌 = pinpai,
       店铺 = shopname,
       物流 = wuliu,
       是否自营 = ziying)
}

# 返回data.frame
hg_rs_jd_product_df = function(page) {
  as.data.frame(hg_rs_jd_product(page),
                stringsAsFactors = FALSE)
}

hg_rs_jdhk_product_df = function (page) {
  title = html_node(page, ".sku-name") %>%
    html_text() %>%
    stringi::stri_replace_all_fixed("\n", "") %>%
    trimws()
  price = html_node(page, ".p-price") %>%
    html_text() %>%
    stringi::stri_replace_all_fixed("￥", "") %>%
    as.numeric()
  pinpai = hg_html_element_text(page, ".shopName a")
  country = hg_html_element_text(page, ".country-name")
  wuliu = ifelse(!is.na(country), country, "全球购")
  isjd = html_node(page, ".u-jd span") %>%
    html_text()
  shopname = hg_html_element_text(page, ".shopName a")
  ziying = ifelse(identical(isjd, "JD"), "自营", "否")
  li = list(标题 = title,
         价格 = as.numeric(price),
         品牌 = pinpai,
         店铺 = shopname,
         物流 = wuliu,
         是否自营 = ziying)
  as.data.frame(li, stringsAsFactors = FALSE)
}

hg_rs_jdhk_pinglun_df = function(page) {
  zongping = hg_html_element_text(page, "li.ui-switchable-item:nth-child(1) > a:nth-child(1) > em:nth-child(1)") %>%
    stringi::stri_replace_all("", regex = "[()+]")


  shaitu = hg_html_element_text(page, "li.ui-switchable-item:nth-child(5) > a:nth-child(1) > em:nth-child(1)") %>%
    stringi::stri_replace_all("", regex = "[^0-9]")
  #zhuiping = html_attr(html_nodes(page, ".filter-list > li:nth-child(3)") , "data-num")
  haoping = hg_html_element_text(page, "li.ui-switchable-item:nth-child(2) > a:nth-child(1) > em:nth-child(1)") %>%
    stringi::stri_replace_all("", regex = "[()+]")
  zhongping = hg_html_element_text(page, "li.ui-switchable-item:nth-child(3) > a:nth-child(1) > em:nth-child(1)") %>%
    stringi::stri_replace_all("", regex = "[()+]")
  chaping = hg_html_element_text(page, "li.ui-switchable-item:nth-child(4) > a:nth-child(1) > em:nth-child(1)") %>%
    stringi::stri_replace_all("", regex = "[()+]")
  haopingdu = hg_html_element_text(page, ".rate > strong:nth-child(2)") %>%
    stringi::stri_replace_all_fixed("%", "") %>%
    as.numeric() /100
  li = lapply(list(总评 = zongping,
                晒图 = shaitu,
                #追评 = zhuiping,
                好评 = haoping,
                中评 = zhongping,
                差评 = chaping,
                好评度 = haopingdu), as.numeric)
  as.tibble(li, stringsAsFactors = FALSE)
}

# 获取产品评论数
hg_rs_jd_pinglun = function(page) {
  zongping = html_attr(html_nodes(page, ".filter-list > li:nth-child(1)") , "data-num")
  shaitu = html_attr(html_nodes(page, ".filter-list > li:nth-child(2)") , "data-num")
  zhuiping = html_attr(html_nodes(page, ".filter-list > li:nth-child(3)") , "data-num")
  haoping = html_attr(html_nodes(page, ".filter-list > li:nth-child(4)") , "data-num")
  zhongping = html_attr(html_nodes(page, ".filter-list > li:nth-child(5)") , "data-num")
  chaping = html_attr(html_nodes(page, ".filter-list > li:nth-child(6)") , "data-num")
  haopingdu = html_node(page, ".percent-con") %>%
    html_text() %>% trimws() %>%
    stringi::stri_replace_all_fixed("%", "") %>%
    as.numeric() /100
  lapply(list(总评 = zongping,
       晒图 = shaitu,
       追评 = zhuiping,
       好评 = haoping,
       中评 = zhongping,
       差评 = chaping,
       好评度 = haopingdu), as.numeric)
}

# 返回data.frame
hg_rs_jd_pinglun_df = function(page) {
  as.tibble(hg_rs_jd_pinglun(page),
                stringsAsFactors = FALSE)
}


hg_rs_jdhk_intro_all_df = function(page) {
  rst = html_nodes(page, ".p-parameter li") %>% html_text() %>%
    strsplit("：", fixed = TRUE) %>%
    unlist() %>% matrix(ncol = 2, byrow = 1) %>%
    as.data.frame(stringsAsFactors = FALSE)
  names(rst) = c("name", "value")
  li = lapply(rst, function(w) {
    trimws(gsub("\n" , "", w))
  }) %>% invisible()
  rst = as.data.frame(li,
                      stringsAsFactors = FALSE) %>%
    column_to_rownames(var = "name") %>%
    t()
  #rownames(rst) = NULL
  as.data.frame(rst, stringsAsFactors = FALSE)
}

# 获取商品所有参数
hg_rs_jd_intro_all = function(page) {
  rst = html_nodes(page, ".p-parameter li") %>% html_text() %>%
    strsplit("：", fixed = TRUE) %>%
    unlist() %>% matrix(ncol = 2, byrow = 1) %>%
    as.data.frame(stringsAsFactors = FALSE)
  names(rst) = c("name", "value")
  rst = lapply(rst, function(w) {
    trimws(gsub("\n" , "", w))
  }) %>% invisible()
  rst
}

# 返回data.frame
hg_rs_jd_intro_all_df = function(page) {
  rst = as.data.frame(hg_rs_jd_intro_all(page),
                      stringsAsFactors = FALSE) %>%
    column_to_rownames(var = "name") %>%
    t()
  #rownames(rst) = NULL
  as.data.frame(rst, stringsAsFactors = FALSE)
}

# 获取商品指定参数
hg_rs_jd_intro = function(page, itemname) {
  rst = hg_rs_jd_intro_all(page) %>%
    #dplyr::filter(name == itemname) %>%
    slice(grep(itemname, name)) %>%
    dplyr::select(value) %>% `[[`(1)
  ifelse(length(rst), rst, NA)
}



# 获取商品几种参数
hg_rs_jd_intro_many = function(page, ...) {
  params = c(...)
  rst = hg_rs_jd_intro_all(page) %>%
    slice(grep(paste(params,collapse="|"), name))
  if(length(rst)) rst
  else NA
}

# 获取产品其他信息
hg_rs_jd_other_info = function(remdr) {
  url = remdr$getCurrentUrl()
  list(
    链接 = url[[1]]
    )
}

# 返回data.frame
hg_rs_jd_other_info_df = function(remdr) {
  as.tibble(hg_rs_jd_other_info(remdr),
            stringsAsFactors = FALSE)
}


hg_jd_is_jdcom = function (page, css = "a.logo") {
  hg_html_element_identical(page, css, "京东")
}

hg_jd_is_jdhk = function (page, css = "a.logo") {
  hg_html_element_identical(page, css, "京东全球购")
}


hg_rs_remote_driver = function(browserName = "phantomjs"
                               , extraCapabilities = list(), ...) {
  if(length(extraCapabilities) == 0) {
    if(browserName == "phantomjs") {
      addCap <- list(phantomjs.page.settings.userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20120101 Firefox/29.0")
    }
  } else {
    addCap = extraCapabilities
  }
  remoteDriver(browserName = browserName, extraCapabilities = addCap, ...)
}

# Rselenium::open
hg_rs_open = function(remdr) {
  has_session = hg_rs_get_sessions_id(remdr)
  id = character(0)
  if (length(has_session) > 1 | is.null(has_session)) {
    try({
      remdr$quit()
      id = remdr$open()
    }, TRUE)
  } else if(length(has_session) == 1){
    has_win = 0
    try({
      has_win = length(remdr$getCurrentWindowHandle())
    }, TRUE)
    if(!has_win) {
      id = remdr$open()
    } else {
      try({id = remdr$getCurrentWindowHandle()},TRUE)
      message("session已存在：")
      message(id)
    }
  }
  if(length(id)) {
    message("\n打开成功！\n")
    1
  } else {
    message("\n打开失败！\n")
    0
  }
}

hg_rs_close = function(remdr) {
  hg_rs_close_(remdr)
  hg_rs_close_(remdr)
  checked = hg_rs_get_sessions_id(remdr)
  message(hg_rs_get_sessions_id(remdr))
  if(is.null(checked)) {
    message("\n关闭成功！\n")
    1
  } else {
    message("\n关闭失败！\n")
    0
  }
}

hg_rs_close_ = function(remdr) {
  try({
    remdr$quit()
    remdr$closeall()
    remdr$close()
    remdr$closeWindow()
  }, TRUE)
}

# 通过Rselenium获取源代码
hg_rs_to_source = function(remdr) {
  message("获取源代码...")
  s = remdr$getPageSource()
  xml2::read_html(s[[1]])
}

# 获取rselenium的session数量
hg_rs_get_sessions_id = function(remdr) {
  message("获取sessions ID...")
  r = remdr$getSessions()
  rst = unlist(map(r, "id"))
  rst
}






#Returns all items in a list that are not contained in toMatch
#toMatch can be a single item or a list of items
exclude <- function (theList, toMatch){
  return(setdiff(theList,include(theList,toMatch)))
}

#Returns all items in a list that ARE contained in toMatch
#toMatch can be a single item or a list of items
include <- function (theList, toMatch){
  matches <- unique (grep(paste(toMatch,collapse="|"),
                          theList, value=TRUE))
  return(matches)
}


# 安装tlmgr包
hg_tlmgr_install_win = function (arg_file, all = FALSE) {
  pkgs = hg_tlmgr_search_win(arg_file)
  message("已搜索到以下包：", pkgs)
  if(!all) {
    s = sub("(.*)[.]{1}(.*)", "\\1", arg_file, perl = TRUE)
    pkgs = pkgs[tolower(pkgs) == tolower(s)]
  }
  message("即将安装以下包：", pkgs)
  for(i in pkgs) {
    tinytex::tlmgr_install(i)
    message("安装【 ", i, " 】成功！\n")
  }
}

hg_tlmgr_search_win = function(arg_file) {
  command = paste0("powershell tlmgr.bat search --global --file ", arg_file)
  out = system(command, intern = TRUE, ignore.stdout = FALSE,
               show.output.on.console = TRUE)
  out = out[grep(".*:$", out)]
  out = stringi::stri_replace_all(out, "", regex = ":")
  out
}




# 判断page:read_html是否存在某个元素
hg_html_has_element = function(page, css) {
  rst = rvest::html_node(page, css)
  ifelse(length(rst), TRUE, FALSE)
}

# 获取page:read_html的元素文本
hg_html_element_text = function(page, css) {
  rst = html_node(page, css)
  html_text(rst) %>%
    stringi::stri_replace_all(regex = "\\s", replacement = "")
}


# 判断page:read_html的tag的文本是否相等
hg_html_element_identical = function(page, css, text) {
  if(hg_html_has_element(page, css)) {
    eltext = hg_html_element_text(page,css)
    checked = identical(as.character(eltext), as.character(text))
    ifelse(checked, TRUE, FALSE)
  } else {
    return(FALSE)
  }
}

# 判断node是否存在属性
hg_html_attr_text = function(page, css, attr_name) {
  node = html_node(page, css)
  attr = html_attr(node, attr_name)
  attr %>% stringi::stri_replace_all(regex = "\\s", replacement = "")
}

# 判断node属性是否等于某文本
hg_html_attr_identical = function(page, css, attr_name, text) {
  attr_text = hg_html_attr_text(page,css,attr_name)
  checked = identical(as.character(attr_text), as.character(text))
  ifelse(checked, TRUE, FALSE)
}

